<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="客户账号" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入账号/姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所在城市" prop="city">
                <el-input v-model="queryParams.city" placeholder="请输入所在城市" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="负责人" prop="responsiblePerson">  
                    <el-select v-model="queryParams.responsiblePerson" placeholder="请选择负责人" clearable @change="handleQuery">
                        <el-option v-for="(dict,i) in rw_user_list" :key="i" :label="dict.label" :value="dict.value" />
                    </el-select>
            </el-form-item>
            <el-form-item label="来源" prop="source">
                <el-select v-model="queryParams.source" placeholder="请选择来源" clearable @change="handleQuery">
                        <el-option v-for="(dict,i) in sourceList" :key="i" :label="dict.label" :value="dict.label" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button size="small" type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['manage:information:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button size="small" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['manage:information:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button size="small" type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['manage:information:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" type="index" width="60" />
            <el-table-column label="账号/姓名" align="center" prop="userName" />
            <el-table-column label="公司名称" align="center" prop="companyName" />
            <el-table-column label="所在城市" align="center" prop="city"/>
            <el-table-column label="手机号" align="center" prop="phoneNumber" />
            <el-table-column label="微信号" align="center" prop="contact" />
            <el-table-column label="旺旺号" align="center" prop="wangWangId" />
            <el-table-column label="来源" align="center" prop="source" />
            <el-table-column label="负责人" align="center" prop="responsiblePerson" />
            <el-table-column label="邮箱" align="center" prop="email" />
            <el-table-column label="地址" align="center" prop="address" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                        <el-link icon="Edit" class="table_link_btn" type="primary" :underline="false" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:content:edit']"><span class="table_link_text">修改</span></el-link>
            <el-link icon="Delete" class="table_link_btn" type="danger" :underline="false" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:content:edit']"><span class="table_link_text">删除</span></el-link>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show=" total > 0 " :total=" total " v-model:page=" queryParams.pageNum "
            v-model:limit=" queryParams.pageSize " @pagination=" getList " />

        <!-- 添加或修改客户信息对话框 -->
        <el-dialog :title=" title " v-model=" open " width="500px" append-to-body>
            <el-form ref="informationRef" :model=" form " :rules=" rules " label-width="80px">
                <el-form-item label="账号/姓名" prop="userName">
                    <el-input v-model=" form.userName " placeholder="请输入账号/姓名" />
                </el-form-item>
                <el-form-item label="公司名称" prop="companyName">
                    <el-input v-model="form.companyName " placeholder="请输入公司名称" />
                </el-form-item>
                <el-form-item label="所在城市" prop="city">
                    <el-input v-model=" form.city " placeholder="请输入所在城市" />
                </el-form-item>
                
                <el-form-item label="手机号" prop="phoneNumber">
                    <el-input v-model=" form.phoneNumber " placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="微信号" prop="contact">
                    <el-input v-model="form.contact " placeholder="请输入联系方式" />
                </el-form-item>
                <el-form-item label="旺旺号" prop="wangWangId">
                    <el-input v-model="form.wangWangId " placeholder="请输入旺旺ID号" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model=" form.email " placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model=" form.address " placeholder="请输入地址" />
                </el-form-item>
                <el-form-item label="负责人" prop="responsiblePerson">
                    <el-select v-model="form.responsiblePerson" filterable class="w-full" placeholder="请选择负责人" >
                        <el-option v-for="library in rw_user_list" :value="library.value" :key="library.value" :label="library.label"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="来源" prop="source">
                    <el-select v-model="form.source" filterable class="w-full" placeholder="请输入来源" >
                        <el-option v-for="item in sourceList" :value="item.label" :key="item.label" :label="item.label"/>
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click=" submitForm ">确 定</el-button>
                    <el-button @click=" cancel ">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Shippers" lang="ts">
import { listShippers, getShippers, delShippers, addShippers, updateShippers } from './api/shippers';
import { getCurrentInstance, reactive, ref, toRefs } from 'vue';

const { proxy } = getCurrentInstance() as any;
const { rw_user_list, rw_shippers_origin} = proxy.useDict('rw_user_list','rw_shippers_origin');
const informationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');
const form = ref<any>({});
const queryParams = ref({
	pageNum: 1,
	pageSize: 10,
	userName: null, city: null,  responsiblePerson: '', source: '',
});
const rules = ref({});
const sourceList = [
	{ label:'1688'},
	{ label:'微信'},
	{ label:'其他'}
];


/** 查询客户信息列表 */
function getList() {
	loading.value = true;
	listShippers(queryParams.value).then(response => {
		informationList.value = response.data.list;
		total.value = response.data.total;
		loading.value = false;
	});
}

// 取消按钮
function cancel() {
	open.value = false;
	reset();
}

// 表单重置
function reset() {
	form.value = {
		id: null, userName: null, city: null, address: null, phoneNumber: null, contact: null, email: null, responsiblePerson: null, source: '1688', companyName:'',wangWangId:''
	};
	proxy.resetForm('informationRef');
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}
const queryRef = ref();
/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm(queryRef);
	handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection:any) {
	ids.value = selection.map((item:any) => item.id);
	single.value = selection.length !== 1;
	multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = '添加客户信息';
}

/** 修改按钮操作 */
function handleUpdate(row:any) {
	reset();
	const id = row.id || ids.value;
	getShippers(id).then(response => {
		form.value = response.data;
		open.value = true;
		title.value = '修改客户信息';
	});
}

/** 提交按钮 */
function submitForm() {
	proxy.$refs['informationRef'].validate((valid:boolean) => {
		if (valid) {
			if (form.value.id != null) {
				updateShippers(form.value).then(response => {
					proxy.$modal.msgSuccess('修改成功');
					open.value = false;
					getList();
				});
			} else {
				addShippers(form.value).then(response => {
					proxy.$modal.msgSuccess('新增成功');
					open.value = false;
					getList();
				});
			}
		}
	});
}

/** 删除按钮操作 */
function handleDelete(row:any) {
	const res = row.id || ids.value;
	proxy.$modal.confirm('是否确认删除客户信息编号为"' + res + '"的数据项？').then(function () {
		return delShippers(res);
	}).then(() => {
		getList();
		proxy.$modal.msgSuccess('删除成功');
	}).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
	proxy.download('manage/information/exportByStream', {
		...queryParams.value
	}, `客户信息_${new Date().getTime()}.xlsx`);
}

getList();
</script>